package com.swdegao.iot.netty.udp.server.guoxinudp;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;

/**
 * 对通信原始数据的切除类
 * @Author:AwakeningCode
 * @Date: Created in 11:40 2018\3\11 0011
 */
public class GuoXinUdpDataResction {


	/**
     * 切除符合协议的数据类型
     * @param data
     * @return
     */
    public static Byte ResctionDataType(Byte dataType){
        return dataType;
    }


	/**
     * 切除符合协议的IMEI和数据域
     * @param data
     * @return
     */
    public static String ResctionIMEIAndBody(String data){
        return data.substring(8,data.length()-6);
    }

    /**
     * 切除符合协议的IMEI
     * @param data
     * @return
     */
    public static byte[] ResctionIMEI(ByteBuf buf){
    	byte[] b = ByteBufUtil.getBytes(buf);
    	byte[] temp = new byte[15];
        System.arraycopy(b, 4, temp, 0, 15);
        return temp;
    }
    /**
     * 切除符合协议的ADD设备地址
     * @param
     * @return
     */
    public static byte[] ResctionADD(ByteBuf buf){
        byte[] b = ByteBufUtil.getBytes(buf);
        byte[] bit_add = new byte[6];
        System.arraycopy(b, 2, bit_add, 0, 6);
        return bit_add;
    }
    public static String ResctionADDStr(ByteBuf buf){
        byte[] bit_add = ResctionADD(buf);
        String temp = getIntFromByte2(bit_add);//终端地址
        return temp;
    }
    /**
     * 两位不足 补零,高位在hou,eg->0x0c 0x02:0c02
     * @param bit
     * @return
     */
    public static String getIntFromByte2(byte[] bit)
    {
        String str = "";
        for (int i = 0; i < bit.length; i++)
        {
            String temp = Integer.toHexString(bit[i]& 0xFF);
            temp = temp.length() < 2 ? ("0" + temp) : temp;
            str = str + temp;
        }
        return str;
    }
    /**
     * 切除符合协议的Body
     * @param data
     * @return
     */
    public static byte[] ResctionBody(ByteBuf buf){
    	byte[] b = ByteBufUtil.getBytes(buf);
    	byte[] temp = new byte[b.length-22];
        System.arraycopy(b, 19, temp, 0, temp.length);
        return temp;
    }

	/**
     * 切除符合协议的数据域
     * @param data
     * @return
     */
    public static String ResctionBody(String data){
        return data.substring(30,data.length());
    }

}
